package com.hnkywl.sts.action.cggl;

import com.hnkywl.sts.entity.cggl.Cgd;
import com.hnkywl.sts.entity.cggl.Cgdmx;
import com.hnkywl.sts.entity.organ.Ck;
import com.hnkywl.sts.entity.organ.Gsjg;
import com.hnkywl.sts.entity.organ.Nbgs;
import com.hnkywl.sts.entity.sys.Gncz;
import com.hnkywl.sts.service.cggl.BgCgdService;
import com.hnkywl.sts.service.cggl.CgdService;
import com.hnkywl.sts.service.htgl.CghtService;
import com.hnkywl.sts.service.organ.GsjgService;
import com.hnkywl.sts.service.sys.GnczService;
import com.hnkywl.sts.service.sys.YgService;
import net.ruixin.web.struts2.SimpleActionSupport;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.springframework.beans.factory.annotation.Autowired;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: jerry
 * Date: 13-4-15
 * Time: 下午2:17
 * To change this template use File | Settings | File Templates.
 */
@Namespace("/cggl/bgcgdgl")
public class BgCgdAction extends SimpleActionSupport {

    // 公司机构service
    private GsjgService gsjgService;

    // 采购单位id
    private Long cgdwId;

    // 采购公司id
    private Long cggsId;

    // 采购部门id
    private Long cgbmId;

    // 采购单号
    private String cgdh;

    // 发货开始时间
    private Date fhrqStart;

    // 发货结束时间
    private Date fhrqEnd;

    // 采购类型
    private String cglx;

    // 采购人id
    private Long cgrId;

    private Long gysid;

    // 状态
    private String zt;

    private String rkzt;

    // 是否提交单
    private String sftzd;

    // 页面当前位置
    private int page;

    // 每页行数
    private int rows;

    private Date sysdate;

    private Date datetime;
    private Integer sfgq;

    // 采购单实体
    private Cgd cgd;

    // 合同类型
    private String htType;

    private String zdr = YgService.getCurrentYgName();
    private Date CurrentTime = new Date();

    //功能操作
    private List<Gncz> gnczList;
    private GnczService gnczService;
    private CghtService cghtService;

    private String wzbm;

    private Date startDate;
    private Date endDate;
    private String wzdl;
    private Long jhdwId;
    private String htbm;

    private CgdService cgdService;
    private BgCgdService bgCgdService;
    private String ewmtime;
    private Long dzckId;
    private Long htId;
    private String wzggIds;
    private String hzmxIds;
    private String htmxIds;
    private String jhsls;
    private Long CurrentYgId;
    private String CurrentYgXm;
    private Long ZgjgId;
    private String ZgjgXm;
    private String jhdhrq;
    private Long cgdId;
    //总公司
    private Nbgs zgs;

    @Action("bgcgdgl")
    public String bgcgdg() {
        gnczList = gnczService.findGnczByJsAndUrl(request.getServletPath());
        return SUCCESS;
    }

    //保供采购单detail
    @Action("bgcgdgl_detail")
    public String bgcgddetail() {
        cgd = this.cgdService.findById(cgd.getId());
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMdd");
        if (cgd != null) {
            ewmtime = simpleDateFormat.format(cgd.getLastUpdated());
        } else {
            ewmtime = "";
        }

        return SUCCESS;
    }


    @Action("bgcgd_into")
    public String bgcgdg_into() {
        Calendar calendar = Calendar.getInstance();
        //得到月初
        calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMinimum(Calendar.DAY_OF_MONTH));
        sysdate = calendar.getTime();
        //得到月末
        calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMaximum(Calendar.DAY_OF_MONTH));
        datetime = calendar.getTime();
        return SUCCESS;
    }

    // 采购单list
    @Action("bgcgdList")
    public void bgcgdList() throws ParseException {
        if (cgdwId != null) {
            Gsjg g = this.gsjgService.findGsjgDetail(cgdwId);
            if (g instanceof Nbgs) {
                cggsId = cgdwId;
            } else {
                cgbmId = cgdwId;
            }
            this.renderJson(this.bgCgdService.findAllBgCgdList(sfgq, wzbm, cgdh, dzckId, fhrqStart, fhrqEnd, cglx, cggsId, cgbmId, cgrId, zt, sftzd, htType, gysid, rkzt, page, rows));
        } else {
            this.renderJson(this.bgCgdService.findAllBgCgdList(sfgq, wzbm, cgdh, dzckId, fhrqStart, fhrqEnd, cglx, cggsId, cgbmId, cgrId, zt, sftzd, htType, gysid, rkzt, page, rows));
        }
    }

    @Action("bgcgdgl_dhyq")
    public String bgcgdglDhyq() {
        cgd = this.cgdService.findById(cgdId);
        SimpleDateFormat simple = new SimpleDateFormat("yyyy-MM-dd");
        jhdhrq = simple.format(cgd.getJhdhrq());
        return SUCCESS;
    }

    @Action("bgcgdSearchList")
    public void bgcgdSearchList() {
        this.renderJson(this.cghtService.findBgjhCght(startDate, endDate, wzdl, wzbm, jhdwId, htbm, gysid, page, rows));
    }

    //保供采购单到货延期
    @Action("bgcgdgl_yq")
    public void bgcgdgl_yq() {
        try {
            this.bgCgdService.saveDhyq(cgd);
            renderText("{\"statusCode\":\"200\",\"message\":\"到货日期延期成功！\"}");
        } catch (RuntimeException e) {
            e.printStackTrace();
            renderText("{\"statusCode\":\"300\", \"message\":\"" + e.getMessage() + "\"}");
        }
    }

    @Action("bgcgdgl_content")
    public String bgcgdcontent() {
        if (cgd != null && cgd.getId() != null) {
            htType = "bght";
            cgd = this.cgdService.findById(cgd.getId());
            List<Cgdmx> cgdmxes = cgd.getCgdmxList();
            hzmxIds = "";
            for (Cgdmx cgdmx : cgdmxes) {
                if (cgdmx.getBgjhhzmxList() != null && cgdmx.getBgjhhzmxList().size() > 0)
                    hzmxIds += (cgdmx.getBgjhhzmxList().get(0).getId().toString() + ",");
            }
            htId = cgd.getCght().getId();
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMdd");
            ewmtime = simpleDateFormat.format(cgd.getLastUpdated());
        } else {
            cgd = new Cgd();
            Ck ck = new Ck();
            ck.setId(dzckId);
            cgd.setDzck(ck);
            ZgjgId = YgService.getCurrentYg().getZgjg().getId();
            ZgjgXm = YgService.getCurrentYg().getZgjg().getJc();
            CurrentYgId = YgService.getCurrentYg().getId();
            CurrentYgXm = YgService.getCurrentYg().getXm();
            sysdate = new Date(); //当前日期
            Calendar calendar = Calendar.getInstance();//日历对象
            calendar.setTime(sysdate);//设置当前日期
            calendar.add(Calendar.MONTH, 1);//将当前日期加一个月
            datetime = calendar.getTime();//输出格式化的日期
        }
        return SUCCESS;
    }

    @Action("bgcgd_add")
    public void bgcgdAdd() {
        try {
            this.bgCgdService.addBgCgd(cgd, hzmxIds);
            renderText("{\"statusCode\":\"200\", \"message\":\"保存采购单成功\"}");
        } catch (Exception e) {
            e.printStackTrace();
            renderText("{\"statusCode\":\"300\", \"message\":\"" + e.getMessage() + "\"}");
        }
    }

    @Action("bgcgd_uppdate")
    public void bgcgdUpdate() {
        try {
            this.bgCgdService.updateBgCgd(cgd, hzmxIds);
            renderText("{\"statusCode\":\"200\", \"message\":\"保存采购单成功\"}");
        } catch (Exception e) {
            e.printStackTrace();
            renderText("{\"statusCode\":\"300\", \"message\":\"" + e.getMessage() + "\"}");
        }
    }

    @Action("bgcgd_del")
    public void bgcgdDel() {
        try {
            this.bgCgdService.deleteBgCgd(cgd);
            renderText("{\"statusCode\":\"200\", \"message\":\"删除采购单成功\"}");
        } catch (Exception e) {
            e.printStackTrace();
            renderText("{\"statusCode\":\"300\", \"message\":\"" + e.getMessage() + "\"}");
        }
    }


    public Integer getSfgq() {
        return sfgq;
    }

    public void setSfgq(Integer sfgq) {
        this.sfgq = sfgq;
    }

    public Long getZgjgId() {
        return ZgjgId;
    }

    public void setZgjgId(Long zgjgId) {
        ZgjgId = zgjgId;
    }

    public String getZgjgXm() {
        return ZgjgXm;
    }

    public void setZgjgXm(String zgjgXm) {
        ZgjgXm = zgjgXm;
    }

    public Long getCgdwId() {
        return cgdwId;
    }

    public Date getStartDate() {
        return startDate;
    }

    public Long getHtId() {
        return htId;
    }

    public void setHtId(Long htId) {
        this.htId = htId;
    }

    public Long getCurrentYgId() {
        return CurrentYgId;
    }

    public void setCurrentYgId(Long currentYgId) {
        CurrentYgId = currentYgId;
    }

    public String getCurrentYgXm() {
        return CurrentYgXm;
    }

    public void setCurrentYgXm(String currentYgXm) {
        CurrentYgXm = currentYgXm;
    }

    public String getWzggIds() {
        return wzggIds;
    }

    public void setWzggIds(String wzggIds) {
        this.wzggIds = wzggIds;
    }

    public void setStartDate(Date startDate) {
        this.startDate = startDate;
    }

    public Date getEndDate() {
        return endDate;
    }

    public void setEndDate(Date endDate) {
        this.endDate = endDate;
    }

    public String getWzdl() {
        return wzdl;
    }

    public void setWzdl(String wzdl) {
        this.wzdl = wzdl;
    }

    public Long getJhdwId() {
        return jhdwId;
    }

    public void setJhdwId(Long jhdwId) {
        this.jhdwId = jhdwId;
    }

    public void setCgdwId(Long cgdwId) {
        this.cgdwId = cgdwId;
    }

    public Long getCggsId() {
        return cggsId;
    }

    public void setCggsId(Long cggsId) {
        this.cggsId = cggsId;
    }

    public Long getCgbmId() {
        return cgbmId;
    }

    public void setCgbmId(Long cgbmId) {
        this.cgbmId = cgbmId;
    }

    public String getCgdh() {
        return cgdh;
    }

    public void setCgdh(String cgdh) {
        this.cgdh = cgdh;
    }

    public Date getFhrqStart() {
        return fhrqStart;
    }

    public void setFhrqStart(Date fhrqStart) {
        this.fhrqStart = fhrqStart;
    }

    public Date getFhrqEnd() {
        return fhrqEnd;
    }

    public void setFhrqEnd(Date fhrqEnd) {
        this.fhrqEnd = fhrqEnd;
    }

    public String getCglx() {
        return cglx;
    }

    public void setCglx(String cglx) {
        this.cglx = cglx;
    }

    public Long getCgrId() {
        return cgrId;
    }

    public void setCgrId(Long cgrId) {
        this.cgrId = cgrId;
    }

    public Long getGysid() {
        return gysid;
    }

    public void setGysid(Long gysid) {
        this.gysid = gysid;
    }

    public String getZt() {
        return zt;
    }

    public void setZt(String zt) {
        this.zt = zt;
    }

    public String getSftzd() {
        return sftzd;
    }

    public void setSftzd(String sftzd) {
        this.sftzd = sftzd;
    }

    public int getPage() {
        return page;
    }

    public String getHtbm() {
        return htbm;
    }

    public void setHtbm(String htbm) {
        this.htbm = htbm;
    }

    public Nbgs getZgs() {
        return zgs;
    }

    public void setZgs(Nbgs zgs) {
        this.zgs = zgs;
    }

    public void setPage(int page) {
        this.page = page;
    }

    public int getRows() {
        return rows;
    }

    public void setRows(int rows) {
        this.rows = rows;
    }

    public Date getCurrentTime() {
        return CurrentTime;
    }

    public void setCurrentTime(Date currentTime) {
        CurrentTime = currentTime;
    }

    public String getZdr() {
        return zdr;
    }

    public void setZdr(String zdr) {
        this.zdr = zdr;
    }

    public Cgd getCgd() {
        return cgd;
    }

    public void setCgd(Cgd cgd) {
        this.cgd = cgd;
    }

    public Date getSysdate() {
        return sysdate;
    }

    public void setSysdate(Date sysdate) {
        this.sysdate = sysdate;
    }

    public String getJhdhrq() {
        return jhdhrq;
    }

    public void setJhdhrq(String jhdhrq) {
        this.jhdhrq = jhdhrq;
    }

    public Date getDatetime() {
        return datetime;
    }

    public void setDatetime(Date datetime) {
        this.datetime = datetime;
    }

    public String getHtType() {
        return htType;
    }

    public void setHtType(String htType) {
        this.htType = htType;
    }

    public String getWzbm() {
        return wzbm;
    }

    public void setWzbm(String wzbm) {
        this.wzbm = wzbm;
    }

    public List<Gncz> getGnczList() {
        return gnczList;
    }

    public void setGnczList(List<Gncz> gnczList) {
        this.gnczList = gnczList;
    }

    public String getHzmxIds() {
        return hzmxIds;
    }

    public String getJhsls() {
        return jhsls;
    }

    public void setJhsls(String jhsls) {
        this.jhsls = jhsls;
    }

    public Long getDzckId() {
        return dzckId;
    }

    public void setDzckId(Long dzckId) {
        this.dzckId = dzckId;
    }

    public void setHzmxIds(String hzmxIds) {
        this.hzmxIds = hzmxIds;
    }

    public String getHtmxIds() {
        return htmxIds;
    }

    public void setHtmxIds(String htmxIds) {
        this.htmxIds = htmxIds;
    }

    public String getEwmtime() {
        return ewmtime;
    }

    public void setEwmtime(String ewmtime) {
        this.ewmtime = ewmtime;
    }

    public String getRkzt() {
        return rkzt;
    }

    public void setRkzt(String rkzt) {
        this.rkzt = rkzt;
    }

    public Long getCgdId() {
        return cgdId;
    }

    public void setCgdId(Long cgdId) {
        this.cgdId = cgdId;
    }

    @Autowired
    public void setGnczService(GnczService gnczService) {
        this.gnczService = gnczService;
    }


    @Autowired
    public void setGsjgService(GsjgService gsjgService) {
        this.gsjgService = gsjgService;
    }

    @Autowired
    public void setBgCgdService(BgCgdService bgCgdService) {
        this.bgCgdService = bgCgdService;
    }

    @Autowired
    public void setCghtService(CghtService cghtService) {
        this.cghtService = cghtService;
    }

    @Autowired
    public void setCgdService(CgdService cgdService) {
        this.cgdService = cgdService;
    }
}


